cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
cmake_policy(VERSION 3.0.0)
if(POLICY CMP0100)
	cmake_policy(SET CMP0100 NEW)
endif()

project(Gapr VERSION 0.1.1.78
	DESCRIPTION "Gapr accelerates projectome reconstruction"
	HOMEPAGE_URL "https://gapr.sourceforge.io/"
	LANGUAGES C CXX)
set(PROJECT_DOMAIN "cn.org.yanlab")
set(APPLICATION_ID "${PROJECT_DOMAIN}.${PROJECT_NAME}")

if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "^Linux|Darwin|Windows$")
	message(FATAL_ERROR "System not supported: ${CMAKE_SYSTEM_NAME}.")
endif()
if(NOT "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "^x86_64|AMD64$")
	message(FATAL_ERROR "CPU not supported: ${CMAKE_SYSTEM_PROCESSOR}.")
endif()
if(CMAKE_CROSSCOMPILING)
	#message(FATAL_ERROR "Cross-compiling not supported.")
endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

if(MSVC)
	#add_compile_options(/W4 /WX)
	add_compile_definitions(_WIN32_WINNT=0x0601)
else()
	add_compile_options(-Wall -Werror)
endif()
if(APPLE)
	#add_link_options("LINKER:-undefined,error")
elseif(MSVC)
else()
	add_link_options("LINKER:--no-undefined")
endif()

option(WITH_SYSTEM_BOOST "Build with system Boost." ON)
option(DEBUG_ASIO "Enable Boost.Asio debugging." OFF)
option(ENABLE_VP9 "Enable VP9 support." ON)
if(ENABLE_VP9)
	option(WITH_SYSTEM_LIBVPX "Build with system libvpx." OFF)
	option(WITH_SYSTEM_LIBWEBM "Build with system libwebm." OFF)
endif()
option(ENABLE_HEVC "Enable HEVC support." ON)
option(ENABLE_GUI "Enable Qt5 GUI support." ON)

# XXX
find_package(OpenMP QUIET)
find_package(Threads REQUIRED)
find_package(ZLIB REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LMDB REQUIRED IMPORTED_TARGET lmdb)
find_package(TIFF REQUIRED)
find_package(OpenSSL REQUIRED COMPONENTS SSL)
find_package(CURL REQUIRED)
if(MSVC)
	find_library(GETOPT_LIBRARY NAMES getopt)
endif()
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("#include <filesystem>\nint main() { return 0; }"
	HAVE_STD_FILESYSTEM)
if(NOT HAVE_STD_FILESYSTEM)
	find_package(Boost 1.70.0 REQUIRED COMPONENTS filesystem)
endif()
check_cxx_source_compiles("#include <charconv>\nint main() { return 0; }"
	HAVE_STD_CHARCONV)

if(WITH_SYSTEM_BOOST)
	find_package(Boost 1.70.0 REQUIRED COMPONENTS context system)
else()
	include("utilities/boost.cmake")
endif()
add_compile_definitions(BOOST_ASIO_DYN_LINK BOOST_ASIO_NO_DEPRECATED)
if(DEBUG_ASIO)
	add_compile_definitions(BOOST_ASIO_ENABLE_HANDLER_TRACKING)
endif()

if(ENABLE_VP9)
	if(WITH_SYSTEM_LIBVPX)
		find_package(VPX, REQUIRED)
	else()
		include("utilities/libvpx.cmake")
	endif()
	if(WITH_SYSTEM_LIBWEBM)
		find_package(WEBM, REQUIRED)
	else()
		include("utilities/libwebm.cmake")
	endif()
endif()

if(ENABLE_HEVC)
	include("utilities/jctvc-hm.cmake")
endif()

if(ENABLE_GUI)
	find_package(Qt5 5.4 REQUIRED COMPONENTS Widgets
		OPTIONAL_COMPONENTS LinguistTools)
	#AC_CHECK_TOOL([LRELEASE], [lrelease-qt5], [lrelease])
	add_compile_definitions(QT_DEPRECATED_WARNINGS)
endif()

include_directories("${CMAKE_BINARY_DIR}/source")
include_directories("${CMAKE_BINARY_DIR}/source/corelib")
include_directories("${CMAKE_SOURCE_DIR}/source/corelib")
include_directories("${CMAKE_SOURCE_DIR}/source/guilib")

include(GNUInstallDirs)

add_subdirectory(source)
#add_subdirectory(doc)
add_subdirectory(packaging)


message(STATUS "---------- Summary ------------")
message(STATUS "${PROJECT_NAME} - ${PROJECT_VERSION}")
message(STATUS "  Build type ................ : ${CMAKE_BUILD_TYPE}")
message(STATUS "  Boost.Asio debugging ...... : ${DEBUG_ASIO}")
message(STATUS "  VP9 Support ............... : ${ENABLE_VP9}")
message(STATUS "  HEVC Support .............. : ${ENABLE_HEVC}")
message(STATUS "  Qt5 GUI ................... : ${ENABLE_GUI}")
message(STATUS "    - translations .......... : ${Qt5LinguistTools_FOUND}")

